package com.hk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.annotation.AutoCheck;
import com.hk.api.imagesearch.ImageSearchApi;
import com.hk.api.imagesearch.vo.ImageVO;
import com.hk.api.outpainting.vo.CreateOutPaintingTaskResponse;
import com.hk.api.outpainting.vo.GetOutPaintingTaskResponse;
import com.hk.auth.SpaceUserPermissionConstant;
import com.hk.auth.annotation.SaSpaceCheckPermission;
import com.hk.common.Constant;
import com.hk.common.ResponseResult;
import com.hk.exception.ErrorCode;
import com.hk.exception.ThrowExceptionUtil;
import com.hk.model.vo.IdVO;
import com.hk.model.vo.picture.*;
import com.hk.service.PictureService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author huangkun
 * @date 2024/12/16 18:40
 */
@RestController
@RequestMapping("/picture")
//@Api(value = "图片管理", tags = "图片管理")
public class PictureController {
    @Resource
    private PictureService pictureService;

    @ApiOperation(value = "上传图片")
    @PostMapping("/upload")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public ResponseResult<PictureVO> upload(@RequestParam("file") MultipartFile file, @RequestParam(value = "fileId", required = false) Long fileId, @RequestParam(value = "spaceId", required = false) Long spaceId) {
        PictureVO pictureVO = pictureService.uploadPicture(file, fileId, null, spaceId);
        return ResponseResult.success(pictureVO);
    }

    @ApiOperation(value = "通过URL上传图片")
    @PostMapping("/upload/url")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public ResponseResult<PictureVO> uploadByUrl(@RequestParam("fileUrl") String fileUrl, @RequestParam(value = "fileId", required = false) Long fileId, @RequestParam(value = "spaceId", required = false) Long spaceId) {
        PictureVO pictureVO = pictureService.uploadPicture(fileUrl, fileId, null, spaceId);
        return ResponseResult.success(pictureVO);
    }

    @ApiOperation(value = "根据id获取图片信息")
    @GetMapping("/get/id")
    public ResponseResult<PictureVO> getPictureInfo(@RequestParam Long id) {
        return ResponseResult.success(pictureService.getPictureInfo(id));
    }

    @ApiOperation(value = "修改图片信息")
    @PostMapping("/update")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public ResponseResult<PictureVO> updatePicture(@RequestBody PictureUpdateRequest updateRequest) {
        return ResponseResult.success(pictureService.updatePicture(updateRequest));
    }

    @ApiOperation(value = "删除图片")
    @PostMapping("/delete")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_DELETE)
    public ResponseResult delete(@RequestBody IdVO idVO) {
        pictureService.delete(idVO);
        return ResponseResult.success();
    }

    @ApiOperation(value = "获取本地空间图片列表")
    @PostMapping("/list")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public ResponseResult<List<PictureVO>> getSpacePictureList(@RequestBody PictureQueryParamVO queryParamVO) {
        return ResponseResult.success(pictureService.getSpacePictureList(queryParamVO));
    }

    @ApiOperation(value = "获取分页列表,管理员使用")
    @PostMapping("/page")
    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    public ResponseResult<Page<PictureVO>> getPicturePage(@RequestBody PictureQueryParamVO queryParamVO) {
        if (queryParamVO.getCurrent() <= 0) {
            queryParamVO.setCurrent(1);
        }

        if (queryParamVO.getPageSize() <= 0) {
            queryParamVO.setPageSize(10);
        }
        if (queryParamVO.getPageSize() > 999) {
            queryParamVO.setPageSize(999);
        }
        return ResponseResult.success(pictureService.getPicturePageList(queryParamVO));
    }

    @ApiOperation(value = "抓取图片资源")
    @GetMapping("/grab")
    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    public ResponseResult<Integer> grabPicture(@RequestParam String searchText, @RequestParam Integer count, @RequestParam(required = false) String namePrefix) {
        return ResponseResult.success(pictureService.grabPicture(searchText, count, namePrefix));
    }

    @ApiOperation(value = "获取分页列表")
    @PostMapping("/page/list")
    public ResponseResult<Page<PictureVO>> getPicturePageList(@RequestBody PictureQueryParamVO queryParamVO) {
        if (queryParamVO.getCurrent() <= 0) {
            queryParamVO.setCurrent(1);
        }

        if (queryParamVO.getPageSize() <= 0) {
            queryParamVO.setPageSize(10);
        }
        if (queryParamVO.getPageSize() > 100) {
            queryParamVO.setPageSize(100);
        }
        return ResponseResult.success(pictureService.getPicturePage(queryParamVO));
    }

    @ApiOperation(value = "审核图片")
    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    @PostMapping("/review")
    public ResponseResult<Boolean> pictureReview(@RequestBody PictureReviewParamVO reviewParamVO) {
        return ResponseResult.success(pictureService.pictureReview(reviewParamVO));
    }

    @ApiOperation(value = "获取分类、标签列表")
    @GetMapping("/tag/list")
    public ResponseResult<PictureTagCategory> getTagList() {
        PictureTagCategory pictureTagCategory = new PictureTagCategory();
        List<String> tagList = Arrays.asList("热门", "搞笑", "生活", "高清", "艺术", "校园", "背景", "简历", "创意");
        List<String> categoryList = Arrays.asList("模板", "电商", "表情包", "素材", "海报");
        pictureTagCategory.setTagList(tagList);
        pictureTagCategory.setCategoryList(categoryList);
        return ResponseResult.success(pictureTagCategory);
    }


    @ApiOperation(value = "获取相似图片列表")
    @GetMapping("/similar/list")
    public ResponseResult<List<ImageVO> > selectSimilarList(Long pictureId) {
        PictureVO pictureInfo = pictureService.getPictureInfo(pictureId);
        ThrowExceptionUtil.throwIf(pictureInfo == null, ErrorCode.ERROR_PARAM, "图片不存在");
        return ResponseResult.success(ImageSearchApi.searchImages(pictureInfo.getUrl()));
    }

    @ApiOperation(value = "创建扩图任务")
    @PostMapping("/create/task")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public ResponseResult<CreateOutPaintingTaskResponse> createOutPaintTask(@RequestBody PictureOutPaintingVO outPaintingVO) {
        return ResponseResult.success(pictureService.createOutPaintTask(outPaintingVO));
    }

    @ApiOperation(value = "获取扩图任务")
    @PostMapping("/get/task")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public ResponseResult<GetOutPaintingTaskResponse> getOutPaintTask(@RequestParam String taskId) {
        return ResponseResult.success(pictureService.getOutPaintTask(taskId));
    }

}
